package com.erp.freight.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.freight.domain.KjxStoreProductRecord;
import com.erp.freight.domain.bo.KjxStoreProductRecordBo;
import com.erp.freight.domain.vo.KjxStoreProductRecordVo;
import com.erp.freight.mapper.KjxStoreProductRecordMapper;
import com.erp.freight.service.IKjxStoreProductRecordService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 熊迪库存记录Service业务层处理
 *
 * @author erp
 * @date 2024-04-07
 */
@RequiredArgsConstructor
@Service
public class KjxStoreProductRecordServiceImpl implements IKjxStoreProductRecordService {

    private final KjxStoreProductRecordMapper baseMapper;

    /**
     * 查询熊迪库存记录
     */
    @Override
    public KjxStoreProductRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询熊迪库存记录列表
     */
    @Override
    public TableDataInfo<KjxStoreProductRecordVo> queryPageList(KjxStoreProductRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KjxStoreProductRecord> lqw = buildQueryWrapper(bo);
        Page<KjxStoreProductRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询熊迪库存记录列表
     */
    @Override
    public List<KjxStoreProductRecordVo> queryList(KjxStoreProductRecordBo bo) {
        LambdaQueryWrapper<KjxStoreProductRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KjxStoreProductRecord> buildQueryWrapper(KjxStoreProductRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KjxStoreProductRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), KjxStoreProductRecord::getSkuCode, bo.getSkuCode());
        lqw.eq(bo.getCount() != null, KjxStoreProductRecord::getCount, bo.getCount());
        lqw.eq(StringUtils.isNotBlank(bo.getWaybillNumber()), KjxStoreProductRecord::getWaybillNumber, bo.getWaybillNumber());
        lqw.eq(bo.getWarehouseId() != null, KjxStoreProductRecord::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getAddTime() != null, KjxStoreProductRecord::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsNo()), KjxStoreProductRecord::getLogisticsNo, bo.getLogisticsNo());
        lqw.eq(StringUtils.isNotBlank(bo.getProof()), KjxStoreProductRecord::getProof, bo.getProof());
        lqw.eq(bo.getPrice() != null, KjxStoreProductRecord::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), KjxStoreProductRecord::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增熊迪库存记录
     */
    @Override
    public Boolean insertByBo(KjxStoreProductRecordBo bo) {
        KjxStoreProductRecord add = BeanUtil.toBean(bo, KjxStoreProductRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改熊迪库存记录
     */
    @Override
    public Boolean updateByBo(KjxStoreProductRecordBo bo) {
        KjxStoreProductRecord update = BeanUtil.toBean(bo, KjxStoreProductRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KjxStoreProductRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除熊迪库存记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public KjxStoreProductRecord getLastApply(String skuCode) {
        return baseMapper.selectOne(new LambdaQueryWrapper<KjxStoreProductRecord>().eq(KjxStoreProductRecord::getSkuCode, skuCode)
                .orderByDesc(KjxStoreProductRecord::getAddTime).last("limit 1"));
    }

}
